package com.enjoy.mjbaseframe.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.Map;

/**
 * 公用工具类
 * Created by Administrator on 2018/3/14.
 */

public class BaseUtil {

    /**
     * 判断是否有网络连接
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * get请求参数配置
     * @param url 请求地址
     * @param map 参数map
     * @return
     */
    public static String jsonToString(String url, Map<String, String> map) {
        if(map == null){
            return url;
        }
        StringBuilder sb = new StringBuilder(url);
        sb.append("?");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            sb.append(key + "=");
            sb.append(value + "&");
        }
        return sb.toString().substring(0, sb.length() - 1);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
